package com.regex;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Group {

	public static void main(String[] args) {
		String s = "Twas brillig, and the slithy toves.\n"
				+ "Did gyre and gimble in the wabe.\n";
		// 匹配最后三个单词
		// 正则表达式中组的概念：组号为0表示整个表达式，组号为1表示第一对括号括起来的组
		Matcher m = Pattern.compile("(?m)(\\S+)\\s+((\\S+)\\s+(\\S+))$")
				.matcher(s);
		while (m.find()) {
			//groupCount()返回该匹配器模式中的分组个数，第0组除外
			for (int j = 0; j < m.groupCount(); j++) {
				System.out.print("[" + m.group(j) + "]");
			}
			System.out.println();
		}
	}

}
